package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.blankj.utilcode.util.GsonUtils
import com.dothantech.lpapi.LPAPI
import com.dothantech.printer.IDzPrinter
import com.dothantech.printer.IDzPrinter.*
import com.gengcon.www.jcprintersdk.JCAPI
import com.gengcon.www.jcprintersdk.callback.Callback
import com.gengcon.www.jcprintersdk.callback.PrintCallback
import com.jt.assistant.R
import com.jt.assistant.adapter.BTAdapter
import com.jt.assistant.bean.BtStatus
import com.jt.assistant.utils.B21PrinterHelper
import com.jt.assistant.viewmodel.HomeViewModel
import com.tencent.mmkv.MMKV
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_bluetooth.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class BluetoothActivity : BaseActivity<HomeViewModel>() {
    private var mCurrentPrinter = ""
    private var mCurrentPrinterBean: BtStatus? = null

    private val mAdapter by lazy {
        BTAdapter(
            ArrayList<BtStatus>()
        )
    }

    private val mBtAdapter: BluetoothAdapter by lazy {
        BluetoothAdapter.getDefaultAdapter()
    }

    override fun onResume() {
        super.onResume()
        checkBT()
        mAdapter.data.clear()
        mAdapter.data.addAll(getBTPairedData())
        mAdapter.notifyDataSetChanged()

        mCurrentPrinter = MMKV.defaultMMKV().getString("CURRENT_PRINTER", "").toString()
        if (mCurrentPrinter.isNotEmpty()) {
            mCurrentPrinterBean = GsonUtils.fromJson(mCurrentPrinter, BtStatus::class.java)
            for (i in mAdapter.data.indices) {
                if (mCurrentPrinterBean!!.address == mAdapter.data[i].address)
                    mAdapter.data[i].status = true
            }
        }
    }

    override fun layoutId(): Int = R.layout.activity_bluetooth

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_bt) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {


    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (!mAdapter.data[position].status) {
                for (i in mAdapter.data.indices) {
                    mAdapter.data[i].status = false
                }
                mAdapter.data[position].status = true
                mAdapter.notifyDataSetChanged()
            }
        }

        bt_commit.setOnClickListener {
            for (i in mAdapter.data.indices) {
                if (mAdapter.data[i].status)
                    MMKV.defaultMMKV()
                        .putString("CURRENT_PRINTER", GsonUtils.toJson(mAdapter.data[i]))
            }
            finish()
        }
    }

    //取得已经配对的蓝牙信息
    private fun getBTPairedData(): List<BtStatus> {
        val btData = ArrayList<BtStatus>()
        // 得到当前的一个已经配对的蓝牙设备
        val pairedDevices = mBtAdapter.bondedDevices

        for (device in pairedDevices) { //遍历
            if (device.name.contains("B11") || device.name.contains("B21"))
                btData.add(BtStatus(device.name, device.address, false))
        }
        return btData
    }

    private fun checkBT() {
        if (!mBtAdapter.isEnabled || getBTPairedData().isEmpty()) {
            showTipDialog()
        }
    }

    private fun showTipDialog() {
        MaterialDialog(this).show {
            title(text = "当前未连接打印机,请点击确定设置蓝牙打印机")
            positiveButton(text = "确定") {
                startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS))
            }
            negativeButton(text = "取消") {}
            lifecycleOwner(this@BluetoothActivity)
        }
    }


}